'use strict';

stareal
    .controller("ViewwangeditorController", function ($scope,$http,$stateParams, $lazyLoader, $api, $alert, $state,localStorageService,$window,$compile,FileUploader,$rootScope) {
        $scope.viewID=$stateParams.id;
        $scope.viewshowname=$stateParams.viewshowname;
        $scope.name="";
        // $("#vieweditorcontainer").height(window.screen.height*70/100);
        var token = localStorageService.get('token')
        //var url = 'https://api.mydeershow.com/mobile/app/upload/image?accessToken='+token;//正式
       var url = 'https://test.djx.api02.opentickets.cn/mobile/app/upload/image?accessToken='+token;//测试
       // var url='http://192.168.1.3/mobile/app/upload/image?accessToken='+token;//正式
        // 全局配置
        // ___E.config.menus = ['bold', 'color', 'quote'];
        //获取可编辑的内容
        // var E = window.wangEditor;
        // var editor = new E('#vieweditorcontainer');
        // // 获取编辑区域的html
        // // var html = editor.$txt.html();
        // // 获取编辑区域的纯文本
        // //var text = editor.$txt.text();
        // editor.customConfig.uploadImgServer = url;  //图片上传
        // editor.customConfig.uploadImgShowBase64 = true;
        // editor.create();

        var editor = new ___E('vieweditortextarea1');
        //
        // // 自定义配置
        editor.config.uploadImgUrl =url;
        //
        // // editor.config.menus = ['bold', 'quote', 'list','img'];
        //
        // // 初始化
        editor.init();


        $scope.$txt = editor.$txt;
        if( $scope.$txt.text()==''){
            $('.wangEditor-mobile-txt').addClass('wang-my-text');
        }
        $('.wangEditor-mobile-txt').on('input', function(){

            if ($scope.$txt.text()!='') {
                $(this).removeClass('wang-my-text');
            } else {
                $(this).addClass('wang-my-text');
            }
        });
        $('.wangEditor-mobile-txt').on('focus', function(event){
            var ua = navigator.userAgent.toLowerCase();
            //console.log($(this));
            if (/iphone/.test(ua)) {
                $(".wangEditor-mobile-menu-container").css('display','block');
                // $(".wangEditor-mobile-menu-container").css('top','120px');
                // $(".wangEditor-mobile-menu-container ").css('position', 'static');
            }
        })
        $('.wangEditor-mobile-txt').on('blur', function(){
            var ua = navigator.userAgent.toLowerCase();
            if (/iphone/.test(ua)) {
                  $(".wangEditor-mobile-menu-container").css('display','none');
            }
        })
        $scope.getDetailContent=function(){
            $api.get("app/view/caoretrievelist",{id:$scope.viewID}, true)
                .then(function(ret) {
                    // 生成编辑器
                    var sContent = ret.data;
                    // console.log(sContent);
                    var newCOntents='';
                    if(sContent.type!=1){
                        var arrContent=JSON.parse(sContent.content);
                        for (var i = 0; i < arrContent.length; i++) {
                            if(arrContent[i].type == 'title'){
                                newCOntents +='<p style="font-weight:bold;font-size: 16px;">'+arrContent[i].title+'</p>';
                            }else if(arrContent[i].type == 'img'){
                                newCOntents +='<img src="'+arrContent[i].img+'">';
                            }else if(arrContent[i].type == 'text'){
                                newCOntents +='<p>'+arrContent[i].text+'</p>';
                            }
                        }
                        sContent.content=newCOntents;
                    }
                    $scope.sContent = sContent;
                    $scope.name=sContent.name;
                   // editor.txt.html($scope.sContent.content);
                    if( $scope.sContent.content==''||$scope.sContent.content==null||$scope.sContent.content==undefined){
                        $('.wangEditor-mobile-txt').addClass('wang-my-text');
                    }else{
                        $scope.$txt.html($scope.sContent.content);
                        $('.wangEditor-mobile-txt').removeClass('wang-my-text');
                    }

                    //  var classifyliId=sContent.classifyid; //分类id
                    //  var useId=sContent.user_id;

                })
        }
        if($scope.viewID!=''){
            $scope.getDetailContent();
        }else{
            if(localStorageService.get("TopicViewcontents")==''||localStorageService.get("TopicViewcontents")==null||localStorageService.get("TopicViewcontents")==undefined){
                $('.wangEditor-mobile-txt').addClass('wang-my-text');
            }else{
                $scope.$txt.html(localStorageService.get("TopicViewcontents"));
                $('.wangEditor-mobile-txt').removeClass('wang-my-text');
            }
            $scope.name=localStorageService.get("TopicViewtitle");

        }

   $scope.clearSine=function(){
       localStorageService.set("TopicViewtitle",'');
       localStorageService.set("TopicViewcontents",'');
       localStorageService.set("viewshowname",'');
       $state.go($rootScope.backname,$rootScope.backname_params,{reload:true});

   }




        // 获取 text
     //   var text = $txt.text();

        // 设置内容
        //$txt.html('自定义设置内容');
         //返回
        $scope.viewwangeditstatus=false;
        $scope.goback_toedit=function(){
            //maiDian('h5-攻略-独家攻略',{{$index+1}},'查看','detail')
            $scope.viewwangeditstatus=true;
            if(_czc!=undefined&&_czc!=null&&_czc!='') {
                _czc.push(["_trackEvent", 'h5-编写观演记-退出', '退出', '查看', '', 'outEdit']);
            }

        }
        //完成
        $scope.nextGo=function(){
            var html =  $scope.$txt.html();
            var text =  ($scope.$txt.text()).trim();
            console.log($scope.name);
            if($scope.name==''||$scope.name==null||$scope.name==undefined){
                $alert.show('请输入标题!');
                return false;
            }
            if(text==''){
                $alert.show('请输入内容!');
                return false;
            }
            localStorageService.set("TopicViewdescriptions",text.slice(0,200));
            localStorageService.set("TopicViewcontents",html);
            localStorageService.set("TopicViewtitle",$scope.name);
            localStorageService.set("TopicViewtextlength",text.length);
            localStorageService.set("viewshowname",$scope.viewshowname);
            $state.go("main.sendView",{id:$scope.viewID},true);

        }
//         $(window).scroll(function(){
// // 重点就是下面这一条语句的实现
//             $("#wangDAILOG").css({top: window.innerHeight + window.scrollY - $("#wangDAILOG").outerHeight() });
//         });
        //获取内容
        $scope.getContent=function(){
                var content= $scope.$txt.html();
                var textlength=0;//文字个数
                var imglength=0;//图片张数
                $scope.description=$scope.$txt.text().slice(0,200);
               $scope.topic_title=$scope.name;
                $scope.position= localStorageService.get("currentPOSITIONID");
                $scope.contents=content;
                // $scope.imglength=imglength;
                // $scope.textlength=textlength;

               // $scope.lengthtime=$('#viewclassifytimes').val();
                return true;


        }
        //保存草稿
        $scope.SaveCaoGaoView=function() {
            if ($scope.getContent()) {
                if($scope.topic_title==''){
                    $alert.show('请输入标题!');
                    return false;
                }
                if( $scope.description==''){
                    $alert.show('请输入内容!');
                    return false;
                }
                var token = localStorageService.get('token');
               // var url = 'https://api.mydeershow.com/mobile/app/view/create?source=pc&accessToken='+token;//正式
               var url = 'https://test.djx.api02.opentickets.cn/mobile/app/view/create?source=pc&accessToken='+token;//测试
                //var url = 'http://192.168.1.3/mobile/app/view/create?source=pc&accessToken=' + token;//测试
                //var url='http://192.168.1.14/mobile/app/view/create?source=pc&accessToken=' + token;//测试
                $http({
                    method: 'post',
                    url: url,
                    data: {
                        id: '',
                        name: $scope.topic_title,
                        thumb: '',
                        content: $scope.contents,
                        describes: $scope.description,
                        position: $scope.position,
                        viewname:$scope.viewshowname,  //观演名称
                        status: -1,
                        type:1, //htmls编辑器
                    },
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    transformRequest: function (data) {
                        return $.param(data);
                    }
                }).then(function successCallback(response) {
                    // $state.go('my.view',{},true);
                    localStorageService.set("TopicViewtitle",'');
                    localStorageService.set("TopicViewcontents",'');
                    localStorageService.set("viewshowname",'');
                    $alert.show("保存成功");
                    $state.go('my.viewcaolist',{},true);
                   // $state.reload();
                }, function errorCallback(response) {
                    $alert.show("保存失败");
                });


            }
        }
        $scope.LeftLength=function () {
            if($scope.name!=''&&$scope.name!=null&&$scope.name!=undefined){
                if($scope.name.length>30){
                    var value = $scope.name.substring(0, 30);
                    //$("#_j_title").attr("value",value);
                    $scope.name = value;
                }
                return 30-$scope.name.length;
            }else{
                return 30;
            }

        }

    })